from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.sms.v20190711 import sms_client, models
from django.conf import settings


def send_message(phone, random_code, tid="856818"):
    try:
        phone = "{}{}".format("+86", phone)
        cred = credential.Credential(settings.TX_SECRETID, settings.TX_SECRETKEY)

        client = sms_client.SmsClient(cred, settings.TX_CITY)

        req = models.SendSmsRequest()

        req.SmsSdkAppid = settings.TX_APP_ID
        # 短信签名内容: 使用 UTF-8 编码，必须填写已审核通过的签名，签名信息可登录 [短信控制台] 查看
        req.Sign = settings.TX_SIGN_NAME

        req.PhoneNumberSet = [phone, ]
        # 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看
        req.TemplateID = tid

        req.TemplateParamSet = [random_code, ]

        resp = client.SendSms(req)

        # 输出json格式的字符串回包
        print(resp.to_json_string(indent=2))

    except TencentCloudSDKException as err:
        print(err)
